package cn.iocoder.yudao.module.wms.service.employee;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;

import cn.iocoder.yudao.module.wms.controller.admin.employee.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.employee.EmployeeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wms.dal.mysql.employee.EmployeeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.*;

/**
 * 员工 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, EmployeeDO> implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    @Override
    public String createEmployee(EmployeeSaveReqVO reqVO) {
        // 通过手机号或者身份证查询员工是否已存在，存在则提示
        boolean exists = this.exists(Wrappers.lambdaQuery(EmployeeDO.class)
                .eq(EmployeeDO::getPhone, reqVO.getPhone().trim())
                .or()
                .eq(EmployeeDO::getIdCard, reqVO.getIdCard().trim())
        );
        if (exists) {
            throw exception(EMPLOYEE_PHONE_OR_ID_CARD_EXISTS);
        }
        // 插入
        EmployeeDO employeeDO = BeanUtils.toBean(reqVO, EmployeeDO.class);
        String now = LocalDateTimeUtil.formatNormal(LocalDateTime.now());
        employeeDO.setId(UUID.randomUUID().toString())
                .setCreateAt(now)
                .setUpdateAt(now);
        employeeMapper.insert(employeeDO);
        // 返回
        return employeeDO.getId();
    }

    @Override
    public void updateEmployee(EmployeeSaveReqVO reqVO) {
        // 校验存在
        validateEmployeeExists(reqVO.getId());
        // 通过手机号或者身份证查询员工是否已存在，存在则提示
        boolean exists = this.exists(Wrappers.lambdaQuery(EmployeeDO.class)
                .ne(EmployeeDO::getId, reqVO.getId())
                .and(o -> o.eq(EmployeeDO::getPhone, reqVO.getPhone().trim())
                        .or()
                        .eq(EmployeeDO::getIdCard, reqVO.getIdCard().trim())
                )
        );
        if (exists) {
            throw exception(EMPLOYEE_PHONE_OR_ID_CARD_EXISTS);
        }
        // 更新
        EmployeeDO updateObj = BeanUtils.toBean(reqVO, EmployeeDO.class);
        updateObj.setUpdateAt(LocalDateTimeUtil.formatNormal(LocalDateTime.now()));
        employeeMapper.updateById(updateObj);
    }

    @Override
    public void deleteEmployee(String id) {
        // 校验存在
        validateEmployeeExists(id);
        // 删除
        employeeMapper.deleteById(id);
    }

    private void validateEmployeeExists(String id) {
        if (employeeMapper.selectById(id) == null) {
            throw exception(EMPLOYEE_NOT_EXISTS);
        }
    }

    @Override
    public EmployeeDO getEmployee(String id) {
        return employeeMapper.selectById(id);
    }

    @Override
    public PageResult<EmployeeDO> getEmployeePage(EmployeePageReqVO pageReqVO) {
        return employeeMapper.selectPage(pageReqVO);
    }

}